# -*- coding: utf-8 -*-

import os
import sys

if __name__ == "__main__":
    currentdir = os.path.dirname( os.path.abspath( sys.argv[0] ) )
    basedir = os.path.abspath( os.path.join( currentdir, ".." ) )
    pythondir = basedir
    sys.path.insert( 0, pythondir )

import random
from jsonrpc.jmsghandler import RequestHandler
from jsonrpc.sendmsg import sendjmsg
from jsonrpc import Server
from time import sleep, time
import traceback
import mimetypes

__all__ = ['CheckFileHandler', 'CheckFileServer', 'CheckFileRequest', 'CheckFileManager']

#---------------------------------------------

class CheckFileHandler(RequestHandler):
    """
    Do some tasks, maybe in subprocess
    """
    
    def _json_handle(self, json_data):
        """
        @param json_data: simple object data, after json.loads
        @type: mixed
        
        @return: python simple object data, jsonable via json.dumps
        @rtype: mixed
        """
        result = False
        
        if not (type(json_data) is dict):
            return result

        if json_data["task"] == "ping":
            return "pong"
        if json_data["task"] == "quit":
            return self.server.quit()
        if json_data["task"] == "check":
            json_data = json_data["data"]
            return self.server.check(
                json_data["dir_id"],
                json_data["parent_path"],
                json_data["name"]
            )
        return result
    
    pass

#---------------------------------------------

class CheckFileServer(Server):
    
    queue = None
    stop_event = None
    task_service_address = None
    _worker_thread = None

    def get_mime_type( self, full_path ):
        mime = None
        try:

            import magic
            mo = magic.open( magic.MAGIC_MIME_TYPE )
            mo.load()
            mime = mo.file( full_path )
            mo.close()

        except Exception:
            pass
        if not mime:
            mime = mimetypes.guess_type( full_path )[0]
        if not mime:
            mime = "application/octet-stream"
        return mime


    def _worker ( self ):

        print("CheckFileServer: START: %s" % (self.server_address,))

        import json
        import fnmatch
        from project.alchemy import Project
        from archive import is_supported
        from service.task import TaskManager

        mimetypes.init()
        mimetypes.add_type( "application/x-rar", ".rar" )
        mimetypes.add_type( "application/x-rar", ".cbr" )
        mimetypes.add_type( "application/zip", ".cbz" )
        mimetypes.add_type( "application/x-7z-compressed", ".7z" )
        mimetypes.add_type( "application/x-7z-compressed", ".cb7" )
        mimetypes.add_type( "application/x-tar", ".cbt" )
        mimetypes.add_type( "application/x-tar", ".cbg" )
        mimetypes.add_type( "application/x-tar", ".cbb" )


        p = Project()
        p.connect()

        task_manager = TaskManager(self.task_service_address)

        stats_req = task_manager.get_request().getStatistics()
        options_req = task_manager.get_request().getOptions()
        hash_req = task_manager.get_request().getHash()
        archive_req = task_manager.get_request().getProcessArchive()

        filter_in = options_req.get("filter_in")
        if filter_in:
            filter_in = json.loads(filter_in)
        else:
            filter_in = []

        filter_out = options_req.get("filter_out")
        if filter_out:
            filter_out = json.loads(filter_out)
        else:
            filter_out = []

        while not self.stop_event.isSet():

            try:
                data = self.queue.get_nowait()
            except Exception:
                data = None

            if data is None:
                sleep(0.01)
                continue


            parent_id, parent_path, name = data

            full_path = os.path.join(parent_path, name)
            if not os.path.isfile(full_path):
                self.queue.task_done()
                continue

            stats_req.inc("files_count", 1)

            match = False
            lpath = full_path.lower()
            for ptrn in filter_in:
                if fnmatch.fnmatchcase(lpath, ptrn.lower()):
                    match = True
                    break
            for ptrn in filter_out:
                if fnmatch.fnmatchcase(lpath, ptrn.lower()):
                    match = False
                    break

            if match:

                stats_req.inc("files_filtered", 1)

                st = os.lstat( full_path )

                try:
                    fsize = st.st_size
                    mtime = st.st_mtime
                    mime = self.get_mime_type( full_path )
                    fitem = p.insert_file( parent_id, st.st_dev, mime, fsize, mtime, name )

                    stats_req.inc("files_size", fsize )

                    fid = fitem.id
                    hash_req.hash( fid, st.st_dev, fsize, full_path )

                    if is_supported(mime):
                        stats_req.inc("archives_count", 1)
                        archive_req.process( fid, full_path, mime )

                except Exception:
                    stats_req.inc("errors_count", 1)
                    print("CheckFileServer: ERROR!\nfile='%s'\n%s" % ( full_path, traceback.format_exc(), ))
                    pass

            self.queue.task_done()

            p.get_session().close()


        p.close_session()

        print("CheckFileServer: STOP: %s" % (self.server_address,))

        self.stop_event.clear()
        return

    def server_activate(self):
        from Queue import Queue
        from threading import Thread, Event

        self.queue = Queue()
        self.stop_event = Event()
        self._worker_thread = Thread(target=self._worker)
        self._worker_thread.start()
        return Server.server_activate(self)

    def check(self, dir_id, parent_path, name):
        self.queue.put_nowait((dir_id, parent_path, name,))
        return True

    def _quit_thread(self):
        sleep(0.05)
        self.server_close()
        return

    def quit(self):
        print("CheckFileServer: quit event")

        self.stop_event.set()

        # Clean Queue

        while True:
            try:
                data = self.queue.get_nowait()
            except Exception:
                data = None

            if data is None:
                break

            self.queue.task_done()

        from threading import Thread

        th = Thread(target = self._quit_thread)
        th.daemon = True
        th.start()
        return True

    pass


#---------------------------------------------

class CheckFileRequest(object):

    manager = None

    def __init__(self, manager):
        self.manager = manager
        pass

    def check(self, device, dir_id, parent_path, name):
        return self.manager.sendmsg( device, "check", {
            "dir_id" : dir_id,
            "parent_path" : parent_path,
            "name" : name
        })

    def ping(self, device):
        return self.manager.sendmsg(device, "ping", {})

    def quit(self):
        return self.manager.quit_all()

    pass

#---------------------------------------------

class CheckFileManager(object):

    host = "127.0.1.5"
    port_min = 10000
    port_max = 20000
    ports = None
    request = None

    task_service_address = None

    def __init__(self, task_service_address):
        self.ports = {}
        self.task_service_address = task_service_address
        pass

    def start_service(self, device):

        device = str(device)

        runing = False
        if device not in self.ports:

            port = random.randint(self.port_min, self.port_max)

            self.ports[device] = port

            from archive import which
            from subprocess import Popen

            Popen([which("python"), "service/check_file.py",
                   "--host", "%s" % self.host,
                   "--port", "%s" % port,
                   "--task-service", "%s:%s" % self.task_service_address
            ])

            # WAIT WHILE SERVICE STARTS

            pings = 10
            t_begin = time()
            while not runing:
                pong = self.sendmsg(device, "ping", {})

                if pong == "pong":
                    runing = True

                pings -= 1
                if pings <= 0:
                    break

            if pings <= 0:
                del self.ports[device]
                print("CheckFileManager: FAIL TO START service on (%s, %s) in %s sec" % (self.host, port, time() - t_begin,))
            else:
                print("CheckFileManager: STARTED service on (%s, %s) in %s sec" % (self.host, port, time() - t_begin,))

        return runing

    def sendmsg(self, device, task, data):
        port = self.ports[str(device)]
        SERVER_ADDRESS = (self.host, port,)

        count = 5
        t_wait = 0.2

        result = None
        while count > 0:
            t_begin = time()
            try:
                result = sendjmsg( SERVER_ADDRESS, {"task":task, "data":data}, timeout=t_wait )
                break
            except Exception as e:
                # print("CheckFileManager[%s]: SOCKET %r" % (count, e,))
                result = None
                count -= 1
                t_end = time()
                if t_end - t_begin < t_wait:
                    sleep(t_wait - (t_end - t_begin))
        if count <= 0:
            print("CheckFileManager: FAIL SEND MESSAGE: task=%s, data=%r" % (task, data,))
        return result

    def quit_all(self):
        result = True
        for device, port in self.ports.items():
            ret = self.sendmsg(device, "quit", {})
            if ret is None:
                result = False
            else:
                del self.ports[ device ]
        return result

    def get_request(self):
        if not self.request:
            self.request = CheckFileRequest(self)
        return self.request

    pass


#---------------------------------------------

def run_check_file_service(host, port, task_service_address):

    try:
        SERVER_ADDRESS = (host, int(port),)

        server = CheckFileServer(SERVER_ADDRESS, CheckFileHandler, bind_and_activate=False)
        if task_service_address:
            host, port = task_service_address.split(':')
            server.task_service_address = (host, int(port),)

        server.server_bind()
        server.server_activate()
        server.serve_forever()
    except Exception:
        pass

    return


#---------------------------------------------

def launch():

    from argparse import ArgumentParser

    parser = ArgumentParser(description='Run file cheking service.')
    parser.add_argument('--host', dest='host', action='store', type=str,
        help='Listen on TCP address')
    parser.add_argument('--port', dest='port', action='store', type=int,
        help='Listen on TCP port')
    parser.add_argument('--task-service', dest='task_service', action='store', type=str,
        help='Task service listen on TCP address')

    args = parser.parse_args()
    run_check_file_service(args.host, args.port, args.task_service)

    return 0

if __name__ == "__main__":
    launch()
